package com.x.planning.model;

import com.x.agent.AgentExecutionStatusEnum;
import com.x.agent.BaseAgent;

/**
 * 单步模型执行model
 * @author xjk
 * @date 2025-06-18
 */
public class PlanModelSingleStep {

  /**
   * 当前步骤id
   */
  Integer stepId;

  /**
   * 总体执行计划id
   */
  String planId;

  /**
   * 步骤执行要求
   */
  String stepRequirement;

  /**
   * 模型返回
   */
  String modelResponseData;

  /**
   * 模型处理结果摘要
   */
  String modelResponseSummary;

  /**
   * 本次参与执行的Agent
   */
  BaseAgent agent;

  public AgentExecutionStatusEnum agentStatus(){
    return agent.getStatus();
  }


  public String getStepInStr() {
    String agentState = null;
    if (agent != null) {
      agentState = agent.getStatus().toString();
    }
    else {
      agentState = AgentExecutionStatusEnum.UN_START.toString();
    }
    StringBuilder sb = new StringBuilder();
    sb.append(stepId);
    sb.append(". ");
    sb.append("[").append(agentState).append("]");
    sb.append(" ");
    sb.append(stepRequirement);

    return sb.toString();
  }

  /**
   * 将步骤转换为JSON字符串
   * @return 步骤的JSON字符串表示
   */
  public String toJson() {
    StringBuilder json = new StringBuilder();
    json.append("    {");
    json.append("\"stepRequirement\": \"").append(stepRequirement.replace("\"", "\\\"")).append("\" ");

    if (modelResponseData != null && !modelResponseData.isEmpty()) {
      json.append(", \"result\": \"").append(modelResponseData.replace("\"", "\\\"").replace("\n", "\\n")).append("\"");
    }

    json.append("}");
    return json.toString();
  }
  /**
   * 从JsonNode解析并创建ExecutionStep对象
   * @param stepNode JsonNode对象
   * @return 解析后的ExecutionStep对象
   */
  public static PlanModelSingleStep fromJson(com.fasterxml.jackson.databind.JsonNode stepNode) {
    PlanModelSingleStep step = new PlanModelSingleStep();

    // 设置步骤需求
    String stepRequirement = stepNode.has("stepRequirement") ? stepNode.get("stepRequirement").asText() : "未指定步骤";
    step.setStepRequirement(stepRequirement);

    // 设置步骤索引（如果有）
    if (stepNode.has("stepIndex")) {
      step.setStepId(stepNode.get("stepIndex").asInt());
    }

    // 设置步骤结果（如果有）
    if (stepNode.has("result")) {
      step.setModelResponseData(stepNode.get("result").asText());
    }

    return step;
  }

  public Integer getStepId() {
    return stepId;
  }

  public void setStepId(Integer stepId) {
    this.stepId = stepId;
  }

  public String getPlanId() {
    return planId;
  }

  public void setPlanId(String planId) {
    this.planId = planId;
  }

  public String getModelResponseData() {
    return modelResponseData;
  }

  public void setModelResponseData(String modelResponseData) {
    this.modelResponseData = modelResponseData;
  }

  public String getModelResponseSummary() {
    return modelResponseSummary;
  }

  public void setModelResponseSummary(String modelResponseSummary) {
    this.modelResponseSummary = modelResponseSummary;
  }

  public BaseAgent getAgent() {
    return agent;
  }

  public void setAgent(BaseAgent agent) {
    this.agent = agent;
  }

  public String getStepRequirement() {
    return stepRequirement;
  }

  public void setStepRequirement(String stepRequirement) {
    this.stepRequirement = stepRequirement;
  }
}
